package com.sfzd5.noipinfo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.google.gson.Gson;

public class UsersActivity extends AppCompatActivity {

    ProgressBar progressBar;
    ListView lv;
    UsersResult result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_users);

        setTitle("团员列表");

        int groupId = getIntent().getIntExtra("groupId", 0);

        progressBar = findViewById(R.id.progressBar);
        lv = findViewById(R.id.lv);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                UsersResult.User g = result.data.get(i);
                Intent intent = new Intent(UsersActivity.this, SubmissionActivity.class);
                intent.putExtra("userId", g.id);
                startActivity(intent);
            }
        });

        progressBar.setVisibility(View.VISIBLE);

        HttpHelper httpHelper = new HttpHelper();
        httpHelper.asyncGetJson("http://ai.sfzd5.com:8800/getusers/?id=" + String.valueOf(groupId), new HttpJsonResult() {
            @Override
            public void tackJson(String json) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        progressBar.setVisibility(View.GONE);
                        if(json == null || json == ""){
                            Toast.makeText(UsersActivity.this,"下载信息失败", Toast.LENGTH_LONG).show();
                            return;
                        }
                        result = new Gson().fromJson(json, UsersResult.class);
                        ArrayAdapter<UsersResult.User> adapter = new ArrayAdapter<UsersResult.User>(
                                UsersActivity.this,
                                android.R.layout.simple_list_item_1,
                                result.data);
                        lv.setAdapter(adapter);
                    }
                });
            }
        });
    }
}